<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<title> MobileAgents1 &lt; Mundo &lt; Foswiki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="icon" href="/foswiki/pub/System/ProjectLogos/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/foswiki/pub/System/ProjectLogos/favicon.ico" type="image/x-icon" />
<link rel="alternate" href="http://127.0.0.1/foswiki/bin/edit/Mundo/MobileAgents1?t=1295995547" type="application/x-wiki" title="edit MobileAgents1" />
<meta name="TEXT_NUM_TOPICS" content="Number of topics:" />
<meta name="TEXT_MODIFY_SEARCH" content="Modify search" />
<meta name="robots" content="noindex" /><link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/foswiki/bin/view/Mundo/WebRss" />
<style type="text/css" media="all">
@import url('style/base.css');
</style>
<style type="text/css" media="all">
@import url('style/layout.css');
@import url('style/style.css');
@import url('style/colors.css');
</style>
<style type="text/css" media="all">
@import url('style/column_left.css');
@import url('style/variant_foswiki_noframe.css');
</style><style type="text/css" media="all">
	/* Styles that are set using variables */
	.patternBookView .foswikiTopRow,
	.patternWebIndicator a img,
	.patternWebIndicator a:hover img {
		background-color:#efefef;
	}
	#patternTopBarContents { background-image:url(/foswiki/pub/System/PatternSkin/header5.gif); background-repeat:repeat-x;}
#patternTopBarContents { background-color:#ffffff;}
.patternBookView {
	border-color:#efefef;
}
.patternPreviewPage #patternMain {
	/* uncomment to set the preview image */
	/*background-image:url("/foswiki/pub/System/PreviewBackground/preview2bg.gif");*/
}
</style>
<style type="text/css" media="all">
@import url('style/print.css');
</style>
<!--[if IE]><style type="text/css" media="screen">
pre {
	height:1%;
	overflow-x:auto;
}
.foswikiAttachments,
.foswikiForm,
.foswikiHelp,
.foswikiPreviewArea,
.patternPreviewPage .foswikiForm,
.patternSigLine,
.patternToolBar,
.patternTop,
.patternTopicAction,
#patternSideBarContents .patternLeftBarPersonal,
#patternSideBarContents h2,
#patternSideBarContents li,
#patternTopBarButtons ul {
	height:1%;
}
#patternSideBarContents .patternLeftBarPersonal {
	width:100%;
}
.foswikiFormStep {
	height:100%;
}
#foswikiLogin,
.patternShadow {
	border:10px solid #fff;
	margin-top:10px;
	margin-bottom:10px;
	border:2px solid #ccc;
}
</style><![endif]-->
<!--[if gt IE 8 ]><style type="text/css" media="screen">
#foswikiLogin,
.patternShadow {
	border:10px solid #fff;
	margin-top:10px;
	margin-bottom:10px;
	box-shadow: 0 0 10px #ccc;
}
</style><![endif]-->
<meta name="foswiki.TWISTYANIMATIONSPEED" content="fast" /><!--TWISTYPLUGIN::META-->
<style type="text/css" media="all">
.foswikiTable {border-width:1px}
.foswikiTable .tableSortIcon img {padding-left:.3em; vertical-align:text-bottom}
.foswikiTable td {border-style:solid none; vertical-align:top}
.foswikiTable th {border-style:none solid; vertical-align:top; background-color:#d6d3cf; color:#000000}
.foswikiTable th a:link {color:#000000}
.foswikiTable th a:visited {color:#000000}
.foswikiTable th a:hover {color:#000000; background-color:#d6d3cf}
.foswikiTable th.foswikiSortedCol {background-color:#c4c1ba}
.foswikiTable tr.foswikiTableRowdataBg0 td {background-color:#ffffff}
.foswikiTable tr.foswikiTableRowdataBg0 td.foswikiSortedCol {background-color:#f7f7f6}
.foswikiTable tr.foswikiTableRowdataBg1 td {background-color:#f7f7f6}
.foswikiTable tr.foswikiTableRowdataBg1 td.foswikiSortedCol {background-color:#f0f0ee}
</style><!--TABLEPLUGIN_default-->
<meta name="foswiki.PUBURL" content="http://127.0.0.1/foswiki/pub" /> <!-- PUBURL -->
<meta name="foswiki.PUBURLPATH" content="/foswiki/pub" /> <!-- PUBURLPATH -->
<meta name="foswiki.SCRIPTSUFFIX" content="" /> <!-- SCRIPTSUFFIX -->
<meta name="foswiki.SCRIPTURL" content="http://127.0.0.1/foswiki/bin" /> <!-- SCRIPTURL -->
<meta name="foswiki.SCRIPTURLPATH" content="/foswiki/bin" /> <!-- SCRIPTURLPATH -->
<meta name="foswiki.SERVERTIME" content="25%20Jan%202011%20-%2023:45" /> <!-- SERVERTIME -->
<meta name="foswiki.SKIN" content="pattern" /> <!-- SKIN -->
<meta name="foswiki.SYSTEMWEB" content="System" /> <!-- SYSTEMWEB -->
<meta name="foswiki.TOPIC" content="MobileAgents1" /> <!-- TOPIC -->
<meta name="foswiki.USERNAME" content="guest" /> <!-- USERNAME -->
<meta name="foswiki.USERSWEB" content="Main" /> <!-- USERSWEB -->
<meta name="foswiki.WEB" content="Mundo" /> <!-- WEB -->
<meta name="foswiki.WIKINAME" content="WikiGuest" /> <!-- WIKINAME -->
<meta name="foswiki.WIKIUSERNAME" content="Main.WikiGuest" /> <!-- WIKIUSERNAME -->
<meta name="foswiki.NAMEFILTER" content="%5b%5cs%5c*%3f~%5e%5c%24%40%25%60%22'%26%3b%7c%3c%3e%5c%5b%5c%5d%23%5cx00-%5cx1f%5d" /> <!-- NAMEFILTER --><!--JQUERYPLUGIN::FOSWIKI::META-->
</head>
<body class="patternViewPage patternPrintPage">
<a name="PageTop"></a><div class="foswikiPage"><div id="patternScreen">
<div id="patternPageShadow">
<div id="patternPage">
<div id="patternOuter">
<div id="patternFloatWrap">
<div id="patternMain">
<div id="patternMainContents">
<div class="patternContent"><div class="foswikiTopic"> <h1><a name="Creating_Autonomous_Agents"></a>  Creating Autonomous Agents </h1>
<p />
Previous: <a href="MobileAgents.html">Mobile Code and Agent Basics</a>
<p />
In this step we will extend the previous example to an autonomous agent, i.e., an agent, that is able to move itself.
<p />
<h3><a name="api"></a>  api </h3>
<p />
Again, the <code>api</code> package contains the interfaces used between the agent and the application. Here, the method <code>run</code> initializes the agent with a <code>name</code> parameter and starts it.
<p />
<strong>samples/agent/agent1/src/api/IMyAgent.java</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> api;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.annotation.mcRemote;

@mcRemote
<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">interface</FONT></B> IMyAgent {
  <B><FONT COLOR="#A020F0">void</FONT></B> run(String name);
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="agent"></a>  agent </h3>
<p />
Now, most of the functionality of this example is implemented in the agent itself. Agents can carry arbitrary data with them. Here, this is demonstrated with the field <code>name</code>. To migrate this data when the agent moves, we declare the class to be serializable with <code>@mcSerialize</code>. Note that serializable fields may not have <code>private</code> visibility. In the code below, the default visibility is used, i.e., the field <code>name</code> will be package private.
<p />
The agent performs the following steps:
<p /> <ol>
<li> The application will create the agent and then call the <code>run</code> method. It initializes the field <code>name</code> and then moves to <code>server1</code>. Because at a <code>moveTo</code> statement, the agent has to suspend its operation on the current node, migrate to another node, and then resume there, the execution of a method cannot continue after a <code>moveTo</code> operation. (Aborting the execution in the middle of a method would be doable, but resuming the execution in the middle of a method would be virtually impossible in Java.) Hence, the code must be split up into multiple methods. The second parameter of <code>moveTo</code> specifies the name of the method that will be invoked at the new location immediately after the service resumes there. Here, the agent is instructed to move to <code>server1</code> and then continue with <code>atServer1</code>.
</li> <li> Arrived at <code>server1</code>, the agent executes method <code>atServer1</code>. It writes a message to the console and then moves on to <code>server2</code>.
</li> <li> Similarly, arrived at <code>server2</code>, the agent executes method <code>atServer2</code> and then moves back to the <code>master</code>.
</li> <li> Finally, arrived at <code>master</code>, the method <code>atMaster</code> is executed.
</li></ol> 
<strong>samples/agent/agent1/src/agent/MyAgent.java</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.Agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.annotation.mcSerialize;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.service.Node;
<B><FONT COLOR="#A020F0">import</FONT></B> api.IMyAgent;

@mcSerialize
<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">class</FONT></B> MyAgent <B><FONT COLOR="#A020F0">extends</FONT></B> Agent <B><FONT COLOR="#A020F0">implements</FONT></B> IMyAgent {
  String name;
  
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> run(String name) {
    <B><FONT COLOR="#A020F0">this</FONT></B>.name = name;
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** &quot;</FONT></B>+name+<B><FONT COLOR="#BC8F8F">&quot; starting at &quot;</FONT></B>+Node.thisNode().getName());
    moveTo(<B><FONT COLOR="#BC8F8F">&quot;server1&quot;</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;atServer1&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> atServer1() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** &quot;</FONT></B>+name+<B><FONT COLOR="#BC8F8F">&quot; now at &quot;</FONT></B>+Node.thisNode().getName());
    moveTo(<B><FONT COLOR="#BC8F8F">&quot;server2&quot;</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;atServer2&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> atServer2() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** &quot;</FONT></B>+name+<B><FONT COLOR="#BC8F8F">&quot; now at &quot;</FONT></B>+Node.thisNode().getName());
    moveTo(<B><FONT COLOR="#BC8F8F">&quot;master&quot;</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;atMaster&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> atMaster() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** &quot;</FONT></B>+name+<B><FONT COLOR="#BC8F8F">&quot; back at &quot;</FONT></B>+Node.thisNode().getName());
  }
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="app"></a>  app </h3>
<p />
<strong>samples/agent/agent1/src/app/MyApp.java</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> app;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.rt.Mundo;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.rt.Service;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.Agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.DoIMobility;
<B><FONT COLOR="#A020F0">import</FONT></B> api.DoIMyAgent;

<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">class</FONT></B> MyApp {
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">static</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> main(String args[]) {
    Mundo.init();
    <B><FONT COLOR="#A020F0">try</FONT></B> {
      Service client = <B><FONT COLOR="#A020F0">new</FONT></B> Service();
      Mundo.registerService(client);

      DoIMobility mobility = Agent.newInstance(client.getSession(), <B><FONT COLOR="#BC8F8F">&quot;agent.MyAgent&quot;</FONT></B>);
      DoIMyAgent agent = <B><FONT COLOR="#A020F0">new</FONT></B> DoIMyAgent(mobility);
      agent.run(<B><FONT COLOR="#BC8F8F">&quot;Agent1&quot;</FONT></B>);
    }
    <B><FONT COLOR="#A020F0">catch</FONT></B>(Exception x) {
      x.printStackTrace();
    }
    Mundo.shutdown();
  }
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="Running_the_example"></a>  Running the example </h3>
<p />
The example uses three MundoCore nodes, named <code>master</code>, <code>server1</code>, and <code>server2</code>. Their working directories reside in <code>var/master</code>, <code>var/server1</code>, and <code>var/server2</code>, respectively. The buildfile will generate the runscripts <code>run-server1</code>, <code>run-server2</code>, and <code>run-master</code> to start these nodes. To run the example, start <code>run-server1</code>, <code>run-server2</code>, and then <code>run-master</code>.
<p /> <ul>
<li> <strong>server1</strong> and <strong>server2</strong> are "empty" <a href="/foswiki/bin/view/Mundo/MundoCore">MundoCore</a> nodes. They simply run <code>org.mundo.util.DefaultApplication</code> from the MundoCore library. The <code>node.conf.xml</code> configuration file in these directories define the node name and the plug-in directory. This is necessary to receive mobile code from peers.
</li> <li> <strong>master</strong> runs the application <strong>MyApp</strong> described above.
</li></ul> 
<p />
<p />
Next: <a href="MobileAgents2.html">Communicating With Local Services</a>
<p />
</div></div><!-- /patternContent-->
</div></div></div></div></div></div></div></div>
</body></html>
